using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using Newtonsoft.Json;
using UnityEngine.UI;

public class NodeBounds
{
    public float top;
    public float bottom;
    public float left;
    public float right;
    public float width;
    public float height;
}

public class NodeMapItem
{
    public int id;
    public int parent;
    public List<NodeMapItem> layers;
}

public class NodeInfoItem
{
    public int id;
    public int parent;
    public string name;
    public string kind;
    public NodeBounds rect;
    public NodeBounds boundsNoEffects;
    public bool visible;
    public int opacity;
    public string blendMode;
    public bool locked;
}

public class UIRoot
{
    public List<NodeMapItem> nodeMap;
    public Dictionary<int,NodeInfoItem> nodeInfos;
}

public static class quickExport{
    
    static UIRoot root;
    [MenuItem("Tools/Export UI %F1")]
    public static void ExportUI()
    {
        Debug.Log("Export UI");
        // string path = "Assets/Scripts/UI/Editor/map.json";
        // string path1 = "Assets/Scripts/UI/Editor/info.json";
        // var text = AssetDatabase.LoadAssetAtPath<TextAsset>(path).text;
        // var text1 = AssetDatabase.LoadAssetAtPath<TextAsset>(path1).text;
        // Debug.Log(text);
        // var map = JsonConvert.DeserializeObject<List<NodeMapItem>>(text);
        // Debug.Log(map.Count);
        // var infos = JsonConvert.DeserializeObject<Dictionary<int,NodeInfoItem>>(text1);
        // Debug.Log(infos.Count);

        string path = "Assets/Scripts/UI/Editor/map.json";
        var text = AssetDatabase.LoadAssetAtPath<TextAsset>(path).text;
        root = JsonConvert.DeserializeObject<UIRoot>(text);

        // find canvas
        var canvas = GameObject.Find("Canvas")?.transform;
        Debug.Log(canvas);
        var UIRoot = new GameObject("UIRoot");
        UIRoot.transform.SetParent(canvas);
        UIRoot.AddComponent<RectTransform>();
        var rt = UIRoot.GetComponent<RectTransform>();
        rt.anchorMin = new Vector2(0,0);
        rt.anchorMax = new Vector2(1,1);
        rt.offsetMin = new Vector2(0,0);
        rt.offsetMax = new Vector2(0,0);
        // rt.pivot = 
        foreach (var item in root.nodeMap)
        {
            var info = root.nodeInfos[item.id];
            GameObject nodeObj;
            if(info.kind == "text")
            {
                nodeObj = DefaultControls.CreateText(new DefaultControls.Resources());
            }else{
                nodeObj = new GameObject(info.name);
                nodeObj.AddComponent<RectTransform>();
                nodeObj.GetComponent<RectTransform>().sizeDelta = new Vector2(info.rect.width,info.rect.height);
                nodeObj.GetComponent<RectTransform>().localPosition = new Vector3(info.rect.left,info.rect.top,0);
            }

            nodeObj.transform.SetParent(rt.transform);

            if(item.layers.Count > 0)
            {
                ExportNode(item.layers,nodeObj);
            }

            
        }
    }

    static void ExportNode(List<NodeMapItem> list,GameObject parent)
    {
        if(list == null) return;
        foreach (var item in list)
        {
            // var node = new GameObject(item.id.ToString());
            // node.transform.SetParent(parent.transform);
            // ExportNode(item.layers,node);

            var info = root.nodeInfos[item.id];
            GameObject node;
            if(info.kind == "text")
            {
                node = DefaultControls.CreateText(new DefaultControls.Resources());
                node.transform.SetParent(parent.transform);
                node.name = info.name;
            }else{
                node = new GameObject(info.name);
                node.transform.SetParent(parent.transform);
                node.AddComponent<RectTransform>();
                node.GetComponent<RectTransform>().sizeDelta = new Vector2(info.rect.width,info.rect.height);
                node.GetComponent<RectTransform>().localPosition = new Vector3(info.rect.left,info.rect.top,0);
            }

            node.GetComponent<RectTransform>().SetParent(parent.GetComponent<RectTransform>());

            if(item.layers.Count > 0)
            {
                ExportNode(item.layers,node);
            }

        }
    }
}
